package com.ruoyi.caigou.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.caigou.domain.dto.BuyReqDTO;
import com.ruoyi.caigou.domain.dto.GoodsDTO;
import com.ruoyi.caigou.domain.dto.IdAndNumber;
import com.ruoyi.system.api.domain.basedata.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.caigou.domain.BuyReq;
import com.ruoyi.caigou.service.IBuyReqService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购申请Controller
 * 
 * @author lvql
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/buyReq")
public class BuyReqController extends BaseController
{
    @Autowired
    private IBuyReqService buyReqService;

    /**
     * 查询采购申请列表
     */
    @RequiresPermissions("caigou:buyReq:list")
    @GetMapping("/list")
    public TableDataInfo list(BuyReq buyReq)
    {
        startPage();
        List<BuyReq> list = buyReqService.selectBuyReqList(buyReq);
        return getDataTable(list);
    }

    /**
     * 导出采购申请列表
     */
    @RequiresPermissions("caigou:buyReq:export")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyReq buyReq)
    {
        List<BuyReq> list = buyReqService.selectBuyReqList(buyReq);
        ExcelUtil<BuyReq> util = new ExcelUtil<BuyReq>(BuyReq.class);
        util.exportExcel(response, list, "采购申请数据");
    }

    /**
     * 获取采购申请详细信息
     */
    @RequiresPermissions("caigou:buyReq:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(buyReqService.selectBuyReqById(id));
    }

    /**
     * 获取采购申请详细信息
     */
//    @RequiresPermissions("caigou:buyReq:query")
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfoPlus(@PathVariable("id") Integer id)
    {
        return success(buyReqService.selectBuyReqByIdPlus(id));
    }

    @PostMapping(value = "/getInfoListByIds")
    public AjaxResult getInfoPlusByIds(@RequestBody List<Integer> ids)
    {
        List<BuyReqDTO> buyReqDTOList = new ArrayList<>();
        for (Integer id : ids) {
            BuyReqDTO buyReqDTO = buyReqService.selectBuyReqByIdPlus(id);
            if (buyReqDTO != null) {
                buyReqDTOList.add(buyReqDTO);
            }
        }
        return success(buyReqDTOList);
    }

    @PostMapping(value = "/getGoodsListByBuyReqIds")
    public AjaxResult getGoodsListByBuyReqIds(@RequestBody List<Integer> ids)
    {
        List<GoodsDTO> goodsDTOList = new ArrayList<>();
        for (Integer id : ids) {
            BuyReqDTO buyReqDTO = buyReqService.selectBuyReqByIdPlus(id);
            String sourceCode = buyReqDTO.getBuyReqDataBase().getCode();
            for (Goods goods : buyReqDTO.getGoodsList()) {
                for (IdAndNumber idAndNumber : buyReqDTO.getIdAndNumberList()) {
                    if (Objects.equals(goods.getId(), idAndNumber.getId())) {
                        GoodsDTO goodsDTO = new GoodsDTO();
                        goodsDTO.setId(goods.getId());
                        goodsDTO.setSourceCode(sourceCode);
                        goodsDTO.setProName(goods.getProName());
                        goodsDTO.setNumber(idAndNumber.getNumber());
                        goodsDTO.setPrice(goods.getPrice());
                        goodsDTO.setUnitName(goods.getUnitName());
                        goodsDTOList.add(goodsDTO);
                    }
                }
            }
        }

        return success(goodsDTOList);
    }

    /**
     * 新增采购申请
     */
    @RequiresPermissions("caigou:buyReq:add")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyReq buyReq)
    {
        return toAjax(buyReqService.insertBuyReq(buyReq));
    }

    /**
     * 新增采购申请
     */
//    @RequiresPermissions("caigou:buyReq:add")
    @Log(title = "采购申请PLUS", businessType = BusinessType.INSERT)
    @PostMapping("/addPlus")
    public AjaxResult addPlus(@RequestBody BuyReqDTO buyReqDTO)
    {
        logger.info("BuyReqController addPlus BuyReqDTO params： {}", buyReqDTO);
        if (buyReqDTO.getIdAndNumberList() == null|| buyReqDTO.getIdAndNumberList().isEmpty()){
            return AjaxResult.error("缺少商品参数");
        }
        return toAjax(buyReqService.insertBuyReqPlus(buyReqDTO));
    }
    /**
     * 修改采购申请
     */
    @RequiresPermissions("caigou:buyReq:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyReq buyReq)
    {
        return toAjax(buyReqService.updateBuyReq(buyReq));
    }

//    @RequiresPermissions("caigou:buyReq:edit")
    @Log(title = "采购申请加强版", businessType = BusinessType.UPDATE)
    @PutMapping("/editPlus")
    public AjaxResult editPlus(@RequestBody BuyReqDTO buyReqDTO)
    {
        logger.info("BuyReqController editPlus BuyReqDTO params： {}", buyReqDTO);
        if (buyReqDTO.getIdAndNumberList() == null ||
                buyReqDTO.getIdAndNumberList().isEmpty() ||
                    buyReqDTO.getBuyReqDataBase().getId() == null){
            return AjaxResult.error("缺少商品参数");
        }
        return toAjax(buyReqService.updateBuyReqPlus(buyReqDTO));
    }

    /**
     * 删除采购申请
     */
    @RequiresPermissions("caigou:buyReq:remove")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(buyReqService.deleteBuyReqByIds(ids));
    }

    /**
     * 删除采购申请
     */
//    @RequiresPermissions("caigou:buyReq:remove")
//    @Log(title = "采购申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/removePlus/{ids}")
    public AjaxResult removePlus(@PathVariable Integer[] ids)
    {
        return toAjax(buyReqService.deleteBuyReqByIdsPlus(ids));
    }

}
